package cn.pconline.autoclub.service;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;

import cn.pconline.autoclub.entity.Photo;
import cn.pconline.autoclub.entity.PhotoReply;
import cn.pconline.autoclub.entity.User;
import cn.pconline.passport2.util.T;

/**
 * 图片审核服务类
 * @author fgs
 *
 */
public class CensorPhotoReplyService extends CensorService{

    // 图片评论typeId,在bean初始化的时候赋值
    private long typeIdPhotoReplyId;
    
    @Autowired
    private PhotoReplyService photoReplyService;
    
    @Autowired
    private AlbumService albumService;
    
	public void setTypeIdPhotoReplyId( long typeIdPhotoReplyId ) {
    	this.typeIdPhotoReplyId = typeIdPhotoReplyId;
    }


	@Override
    public boolean isCorrectFeedback( String url ) {
    	Map<String,Long> map  = this.getMapByUrl(url);
    	long typeId = 0;
    	if(map!=null&&!map.isEmpty()){
    		typeId = map.get("typeId");
    	}
        return typeIdPhotoReplyId == typeId;
    }

	
    /**
     * 发图片评论时,通知审核平台新建留言状态
     * @param request
     * @param photoReply
     */
    public void notifyCensorSystemCreate(HttpServletRequest request,PhotoReply photoReply){
    	if (csr_switch){
    		 Map<String, Object> params = convert4Create(request, photoReply,true);
    		 executorService.submit(new CensorCall(receiveHome, params));
    	}
    }
    
    /**
     * 向审核系统传递的参数，用户图片评论
     * @param request
     * @param photoReply
     * @param isNew
     * @return
     */
    private  Map<String, Object> convert4Create(HttpServletRequest request,PhotoReply photoReply,boolean isNew){
    	User user = null;
        try {
            user = this.geliDao.find(User.class, photoReply.getCreateBy());
        } catch (DataAccessException e) {
        }
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put(TITLE, photoReply.getContent());
        map.put(SITE, site);
        map.put(APP, appName);
        map.put(TYPEID, typeIdPhotoReplyId);
        map.put(ISNEW, isNew ? 1 : 0);
        String root = "http://" + T.stringValue(request.getHeader("host"), "") + T.stringValue(request.getContextPath(), "");
        map.put(URI, root + "/album/photoDetail.do?photoReplyId=" + photoReply.getPhotoReplyId() + "&albumId=" + photoReply.getAlbumId() + "&photoId="
                + photoReply.getPhotoId()+"&typeId="+typeIdPhotoReplyId);
        map.put(VERSION, (photoReply.getCreateAt().getTime() / 1000) * 1000);
        map.put(USERID, user.getUid());
        map.put(USERNAME, user.getUsername());
        map.put(IP, T.getIp(request));
        map.put(CONTENT, photoReply.getContent());
        return map;
        
    }
    
    /**
     * 审核回调
     * @param interfaceUrl
     * @param url
     * @param status
     * @param version
     * @return
     */
    public String feedback(String interfaceUrl, String url, int status,
            long version){
        String result = null;
        // url must format like : http://my.pcauto.com.cn/2?typeId=203
        Map<String,Long> map  = this.getMapByUrl(url);
        long albumId = map.get("albumId");
        long photoId = map.get("photoId");
        long photoReplyId = map.get("photoReplyId");
        long typeId = map.get("typeId");
        if (typeId == typeIdPhotoReplyId){
        	PhotoReply photoReply = null;
        	Photo photo = null;
        	try{
        		photoReply = photoReplyService.findPhotoReplyById( photoReplyId, photoId );
            	photo = albumService.getPhotoById( photoId, albumId );
        	}catch (EmptyResultDataAccessException e) {
        		 log.info("审核平台回调图片评论已不存在: albumId:{} , photoId:{}",albumId,photoId );
			}
        	if(photoReply!=null && (version == photoReply.getCreateAt().getTime())){
                switch (status) {
                    //审核通过?
                    case CENSORSYSTEM_RESPONSE_PASS:
                        photoReplyService.censorPhotoReply( photo, photoReply, PhotoReply.STATUS_NORMAL );
                        break;
                    //审核不通过    
                    case CENSORSYSTEM_RESPONSE_DENY:
                    	photoReplyService.censorPhotoReply( photo, photoReply, PhotoReply.STATUS_DELETE);
                        log.info("图片id{} 的评论 id{} 审核不通过：",photo.getPhotoId(),photoReply.getPhotoReplyId() );
                        break;
                    default:
                        result = "状态不正确";
                        break;
                }
        	}
        }
        return result;
    }
    
}
